<template>
    <section>

        <!--列表-->
        <el-table :data="shop" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="shop.state" label="审核状态" width="100" sortable>
                <template scope="scope">
                    <span style="color: red" v-if="scope.row.state==0">待审核</span>
                    <span style="color: blue" v-else-if="scope.row.state==1">待激活</span>
                    <span style="color: green" v-else-if="scope.row.state==2">正常</span>
                    <span style="color: gray" v-else-if="scope.row.state==-1">被驳回</span>
                </template>
            </el-table-column>
            <!--<el-table-column prop="tel" label="店铺logo" width="100" sortable>
            </el-table-column>-->


            <el-table-column label="操作" width="400">
                <template scope="scope">
                <el-button  size="small" @click="handleInfoForm(scope.$index, scope.row)">查看详情</el-button>


                <span v-if="scope.row.state==-1">
                    <el-button  size="small" @click="handleReturnForm(scope.$index, scope.row)">驳回原因</el-button>
                    <el-button  size="small" @click="updateReturnForm(scope.$index, scope.row)" >修改原因</el-button>
                </span>
                <span v-else></span>

                    <el-button  size="small" @click="getShopAll(scope.$index, scope.row)">刷新状态</el-button>
                </template>

            </el-table-column>
        </el-table>


        <!--驳回原因对话框-->
        <el-dialog :title="title" :visible.sync="returnFormVisible" :close-on-click-modal="false">
            <el-form :model="returnForm" label-width="80px"  ref="returnForm">
                <template scope="scope">
                    <el-form-item label="驳回原因">
                        <el-input type="textarea" v-model="returnForm.content"></el-input>
                    </el-form-item>
                </template>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="returnFormVisible = false">关闭</el-button>
            </div>
        </el-dialog>

        <!--驳回原因修改对话框-->
        <el-dialog :title="title" :visible.sync="updateFormVisible" :close-on-click-modal="false">
            <el-form :model="updateForm" label-width="80px"  ref="updateForm">
                <template scope="scope">
                    <el-form-item label="驳回原因">
                        <el-input type="textarea" v-model="updateForm.content"></el-input>
                    </el-form-item>
                </template>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="updateFormVisible = false">关闭</el-button>
                <el-button type="primary" @click.native="saveSubmit" :loading="updateFormVisibleLoading">提交</el-button>
            </div>
        </el-dialog>

        <!-- 查看详情 -->
        <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
            <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
                <el-form-item label="店铺名称" prop="name">
                    <el-input v-model="saveForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="tel">
                    <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="注册时间" prop="registerTime">
                    <el-input v-model="saveForm.registerTime" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="saveForm.state" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveFormVisible = false">关闭</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/js/util'

    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                shops: [],
                shop: [],
                totals: 0,
                currentPage: 1,
                pageSize:5,
                listLoading: false,
                sels: [],//列表选中列
                title: "",
                managers:[],
                deptTree:[],

                //编辑界面数据清空
                saveForm: {
                    id: null,
                    state: -1,
                    content:''
                },
                //编辑界面数据清空
                returnForm: {
                    id: null,
                    state: -1,
                    content:''
                },
                //编辑界面数据清空
                updateForm: {
                    id: null,
                    state: -1,
                    content:''
                },

                //新增模态框模型
                saveFormVisible: false,//新增界面是否显示
                saveLoading: false,
                saveFormRules: {
                },
                //新增模态框模型
                returnFormVisible: false,//新增界面是否显示
                returnFormVisibleLoading: false,
                returnFormRules: {
                },
                //新增模态框模型
                updateFormVisible: false,//新增界面是否显示
                updateFormVisibleLoading: false,
                updateFormRules: {
                },


            }
        },
        methods: {
            highQuery(){
                this.currentPage = 1;
                this.getShops();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getShops();
            },
            //获取门店列表
            getShops() {
                this.listLoading = true;
                this.$http.get("/shop/getInfo")
                    .then(res=>{
                        //1.去掉忙等框
                        this.listLoading = false;
                        //2.给模型数据shops赋值
                        this.shops = res.data.data;
                    })
                    .catch(res=>{
                        /*alert("系统错误");*/
                        this.listLoading = false;
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = "/shop/" + row.id;
                    this.$http.delete(para)
                        .then(res=>{
                            this.listLoading = false;
                            if(res.data.success) {
                                this.$message({
                                    message:"删除成功",
                                    type:'success'
                                })
                                //如果删除的是本页最后一条且当前页不是第一页，就跳转到上一页
                                if(this.currentPage>1 && (this.totals - 1)% this.pageSize == 0){
                                    this.currentPage = this.currentPage -1
                                }
                                this.getShops();
                            } else {
                                this.$message({
                                    message:res.data.msg,
                                    type:'error'
                                })
                            }
                        })
                        .catch(res=>{
                            alert("系统异常")
                        })
                }).catch(() => {

                });
            },
            //显示查看详情界面
            handleInfoForm: function (index, row) {
                this.title = "详情"
                this.saveForm = Object.assign({}, row);
                this.saveFormVisible = true;
            },
            //显示驳回原因界面
            handleReturnForm: function (index, row) {
                this.title = "驳回原因"
                this.returnForm = Object.assign({}, row);
                this.returnFormVisible = true;
            },
            //显示修改驳回原因界面
            updateReturnForm: function (index, row) {
                this.title = "修改"
                this.updateForm = Object.assign({}, row);
                this.updateFormVisible = true;
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增"
                this.getManagers();
                this.getDeptTrees()
                this.saveFormVisible = true;
                this.saveForm = {
                    name: '',
                    sn: "",
                    state:0,
                    manager:null,
                    parent:null
                };
            },

            saveSubmit: function () {
                this.$refs.updateForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.updateFormVisibleLoading = true;
                            // 克隆表单数据到空{}，防止原对象数据被修改
                            let para = Object.assign({}, this.updateForm);
                            console.log(para)

                            this.$http.put("/shop/setLoding",para)
                                .then(res=>{
                                    this.this.updateFormVisibleLoading = false;
                                    console.log(para);
                                    if(res.data.success) {
                                        this.$message({
                                            message:"修改成功，请您耐心等候呢",
                                            type:'success'
                                        })
                                        this.$refs['updateForm'].resetFields();
                                        this.updateFormVisible = false;
                                        this.getShops();
                                    } else {
                                        this.$message({
                                            message:res.data.msg,
                                            type:'error'
                                        })
                                    }
                                })
                                .catch(res=>{
                                    /*alert("系统异常")*/
                                    this.updateFormVisible = false;
                                    this.getShops();
                                })

                        });
                    }
                });
            },

            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/shop",ids)
                        .then(res=>{
                            this.listLoading = false;
                            if (res.data.success) {
                                this.$message({
                                    message:"操作成功",
                                    type:'success'
                                })
                                this.currentPage = 1;
                                this.getShops();
                            } else{
                                this.$message({
                                    message:res.data.msg,
                                    type:'error'
                                })
                            }
                        })
                        .catch(res=>{
                            alert("系统异常！")
                        })
                }).catch(() => {
                });
            },

            setPass: function (index, row) {
                let para = "/shop/" + row.id;
                this.$http.delete(para)
                    .then(res=>{
                        /*this.listLoading = false;*/
                        console.log(para);
                        if(res.data.success) {
                            this.$message({
                                message:"审核通过",
                                type:'success'
                            })
                            this.getShops();
                        } else {
                            this.$message({
                                message:res.data.msg,
                                type:'error'
                            })
                        }
                    })
                    .catch(res=>{
                        alert("系统崩溃")
                    })
            },
            setReturn: function (index, row) {
                let para = "/shop/setReturn/" + row.id;
                this.$http.delete(para)
                    .then(res=>{
                        console.log(para);
                        if(res.data.success) {
                            this.$message({
                                message:"驳回成功",
                                type:'success'
                            })
                            this.getShops();
                        } else {
                            this.$message({
                                message:res.data.msg,
                                type:'error'
                            })
                        }
                    })
                    .catch(res=>{
                        alert("系统崩溃")
                    })
            },
            getShopAll(){
                this.$http.get("/login/getInfo")
                    .then(res=>{
                        if (res) {
                            this.shop = res.data;
                        }
                    })
            },
        },

        mounted() {
            this.getShopAll();
        }
    }

</script>

<style scoped>

</style>